package com.irononetech.android.MultilevelListView;

import java.util.Arrays;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import android.app.AlertDialog;
import android.app.ExpandableListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.irononetech.android.Application.Application;
import com.irononetech.android.claimsone.R;
import com.irononetech.android.formcomponent.FormObject;
import com.irononetech.android.formcomponent.view.VehicleDetailsActivity;

public class ExpList3_Motorcycle extends ExpandableListActivity {

	Logger LOG = LoggerFactory.getLogger(ExpList3_Motorcycle.class);

	private ColorExpListAdapterMotorcycle colorExpListAdapter;
	FormObject fo;
	static boolean booleanlistMotorcycle[][][][];
	static boolean booleanprelistMotorcycle[][][][];

	boolean booleanlistMotorcycleCopy[][][][];
	boolean booleanprelistMotorcycleCopy[][][][];
	int p1, p2, p3, p4;
	Button resetBtn;
	LinearLayout damageditems_header;

	EditText otherfield;

	static final String listdesc[][][][] = { { { { "Motorcycle", "All parts" },
			{ "Head Light Lh", "" }, { "Head Light", "" },
			{ "Head Light Cowling", "" }, { "Visor", "" }, { "Meter", "" },
			{ "Handle", "" }, { "Front R/H Signal Light", "" },
			{ "Front L/H Signal Light", "" }, { "Front R/H Mirror", "" },
			{ "Front L/H Mirror", "" }, { "Clutch Lever", "" },
			{ "Break Lever", "" }, { "Clutch cable", "" },
			{ "Break cable", "" }, { "Ignition Switch", "" },
			{ "Handle Switch Left", "" }, { "Handle Switch Right", "" },
			{ "Front Fork Tube", "" }, { "Front Fork Seal", "" },
			{ "Petrol Tank", "" }, { "Petrol Tank Lid", "" },
			{ "P/Tank cowling (L/R)", "" }, { "T Column", "" },
			{ "Top Column", "" }, { "Handle Bolt", "" },
			{ "Handle cup set upper", "" }, { "Handle cup set Lover", "" },
			{ "Speedometer", "" }, { "Speedometer cable", "" },
			{ "Front Hub", "" }, { "Front Disk", "" }, { "Wheel Rim", "" },
			{ "Alloy Wheel Rim", "" }, { "Liver set Front", "" },
			{ "Front Tire", "" }, { "Front Tire Tube", "" },
			{ "Front Fender", "" }, { "Seat", "" },
			{ "Seat Cowling (L/R)", "" }, { "Handle Balancer", "" },
			{ "Seat Grip", "" }, { "Right Side Cover", "" },
			{ "Left side cover", "" }, { "Kick Paddle", "" },
			{ "Break Paddle", "" }, { "Gear Lever", "" },
			{ "Right side Front Footrest", "" },
			{ "Right side Rear Footrest", "" },
			{ "Left side Front Footrest", "" },
			{ "Left side Rear Footrest", "" }, { "Foot-rest Rubber", "" },
			{ "Sari Guard", "" }, { "Swing Arm", "" }, { "Silencer", "" },
			{ "Silencer Guard", "" }, { "Crash Bar", "" },
			{ "Right Fender", "" }, { "Rear Right Signal Light", "" },
			{ "Rear Left signal Light", "" }, { "Break Light", "" },
			{ "Rear Wheel Rim", "" }, { "Right Alloy Wheel Rim", "" },
			{ "Rear Tire", "" }, { "Rear Tire Tube", "" }, { "Chain", "" },
			{ "Spoke", "" }, { "Chain Case", "" }, { "Main Stand", "" },
			{ "Side Stand", "" }, { "Battery", "" },
			{ "Footrest Holder RH", "" }, { "Footrest Holder LH", "" },
			{ "Tank Sticker set", "" }, { "Tail cover Sticker LH/RH", "" },
			{ "R Shock Absorber (LH/RH)", "" }, { "Front panel Top", "" },
			{ "Front panel Bottom", "" }, { "Rear RH Panel Bottom", "" },
			{ "Rear LH Panel Bottom", "" }, { "Rear RH Panel Top", "" },
			{ "Rear LH Panel Top", "" }, { "Break caliper", "" },
			{ "Fork Oil", "" }, { "Head Light Bracket", "" },
			{ "R Additional Cover", "" }, { "Starter Motor", "" },
			{ "Break servo Tank", "" }, { "Chock Cable", "" },
			{ "Number Plate", "" }, { "Side panels (for scooters)", "" },
			{ "Top panels (for scooters)", "" },
			{ "v-panels (for scooters)", "" },
			{ "Center panels (for scooters)", "" },
			{ "R/S bottom panels (for scooters)", "" },
			{ "L/S bottom pales (for scooters)", "" },
			{ "Front buckets (for scooters)", "" }, { "Back rests", "" },
			{ "Rear crash bars", "" }, { "CDI unit", "" }, }, } };

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle icicle) {
		try {
			super.onCreate(icicle);
			LOG.debug("ENTRY onCreate");
			setContentView(R.layout.multilevel_expandable_list_main);
			fo = Application.getFormObjectInstance();
			otherfield = (EditText) findViewById(R.id.damagedItemsOtherField);

			resetBtn = (Button) findViewById(R.id.resetButton);
			damageditems_header = (LinearLayout) findViewById(R.id.damageditems_header);
			
			p1 = Application.get4DArrSizeSec1();
			p2 = Application.get4DArrSizeSec2();
			p3 = Application.get4DArrSizeSec3();
			p4 = Application.get4DArrSizeSec4();
			
			if (fo.getisPreSelected()) // PRE
			{
				otherfield.setText(fo.getpredamagedItemsOtherField());
				booleanprelistMotorcycle = Arrays.copyOf(fo.getBooleanprelistMotorcycle(), fo.getBooleanprelistMotorcycle().length);
				
				booleanprelistMotorcycleCopy = new boolean[p1][p2][p3][p4];
				for (int i = 0; i < p1; i++) {
					for (int j = 0; j < p2; j++) {
						for (int k = 0; k < p3; k++) {
							booleanprelistMotorcycleCopy[i][j][k][0] = booleanprelistMotorcycle[i][j][k][0];
							booleanprelistMotorcycleCopy[i][j][k][1] = booleanprelistMotorcycle[i][j][k][1];
						}
					}
				}
				colorExpListAdapter = new ColorExpListAdapterMotorcycle(this, getExpandableListView(), listdesc);
				setListAdapter(colorExpListAdapter);
				damageditems_header.setBackgroundDrawable(getResources().getDrawable(R.drawable.predamageditems_header));
			} else {
				otherfield.setText(fo.getdamagedItemsOtherField());
				booleanlistMotorcycle = Arrays.copyOf(fo.getBooleanlistMotorcycle(), fo.getBooleanlistMotorcycle().length);
				
				booleanlistMotorcycleCopy = new boolean[p1][p2][p3][p4];
				for (int i = 0; i < p1; i++) {
					for (int j = 0; j < p2; j++) {
						for (int k = 0; k < p3; k++) {
							booleanlistMotorcycleCopy[i][j][k][0] = booleanlistMotorcycle[i][j][k][0];
							booleanlistMotorcycleCopy[i][j][k][1] = booleanlistMotorcycle[i][j][k][1];
						}
					}
				}
				colorExpListAdapter = new ColorExpListAdapterMotorcycle(this, getExpandableListView(), listdesc);
				setListAdapter(colorExpListAdapter);
				damageditems_header.setBackgroundDrawable(getResources().getDrawable(R.drawable.damageditems_header));
			}
			
			if (!fo.isEditable()) {
				editDisable();
			}
			
			LOG.debug("SUCCESS onCreate");
		} catch (Exception e) {
			LOG.error("editEnable:10320");
			if (e != null) {
				LOG.error("Message: " + e.getMessage());
				LOG.error("StackTrace: " + e.getStackTrace());
			}
		}
	}

	public void editDisable() {
		try {
			otherfield.setFocusable(false);
			resetBtn.setVisibility(View.GONE);
			
			/*if (fo.getisPreSelected()) // PRE
			{
				back.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.back_button_xml));
			} else {
				back.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.back_button_xml));
			}*/
		} catch (Exception e) {
			LOG.error("editEnable:10322");
			if (e != null) {
				LOG.error("Message: " + e.getMessage());
				LOG.error("StackTrace: " + e.getStackTrace());
			}
		}
	}

	/*public void editEnable() {
		try {
			otherfield.setFocusable(true);

			if (fo.getisPreSelected()) // PRE
			{
				back.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.back_button_xml));
			} else {
				back.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.cancel_button_xml));
			}
		} catch (NotFoundException e) {
			LOG.error("editEnable:10323");
			if (e != null) {
				LOG.error("Message: " + e.getMessage());
				LOG.error("StackTrace: " + e.getStackTrace());
			}
		} catch (Exception e) {
			LOG.error("editEnable:10324");
			if (e != null) {
				LOG.error("Message: " + e.getMessage());
				LOG.error("StackTrace: " + e.getStackTrace());
			}
		}
	}*/

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}

	public void multilevel_ok_click(View v) {
		okCommon();
	}

	private void okCommon() {
		try {
			if (fo.getisPreSelected()) // PRE
			{
				fo.setBooleanprelistMotorcycle(booleanprelistMotorcycle);
				fo.setpredamagedItemsOtherField(otherfield.getText().toString().trim());
			} else {
				fo.setBooleanlistMotorcycle(booleanlistMotorcycle);
				fo.setdamagedItemsOtherField(otherfield.getText().toString().trim());
			}
			Application.setFormObjectInstance(fo);
			
			Intent mIntent = new Intent(ExpList3_Motorcycle.this, VehicleDetailsActivity.class);
			mIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
			startActivity(mIntent);
			finish();
		} catch (Exception e) {
			LOG.error("okCommon:10301");
			if (e != null) {
				LOG.error("Message: " + e.getMessage());
				LOG.error("StackTrace: " + e.getStackTrace());
			}
		}
	}
	
	private void backCommon() {
		try {
			if (fo.getisPreSelected()) // PRE
			{
				fo.setpredamagedItemsOtherField(fo.getpredamagedItemsOtherField());
				fo.setBooleanprelistMotorcycle(booleanprelistMotorcycleCopy);
			} else {
				fo.setdamagedItemsOtherField(fo.getdamagedItemsOtherField());
				fo.setBooleanlistMotorcycle(booleanlistMotorcycleCopy);
			}
			Application.setFormObjectInstance(fo);
		} catch (Exception e) {
			LOG.error("backCommon:13388");
			if (e != null) {
				LOG.error("Message: " + e.getMessage());
				LOG.error("StackTrace: " + e.getStackTrace());
			}
		}
	}
	
	public void multilevel_back_button(View v) {
		try {
			if (fo.isEditable()) {
				
				boolean isSameList = true;
				boolean isSameText = false;
				
				if (fo.getisPreSelected()) // PRE
				{
					//isSameList = Arrays.equals(booleanprelistBus, booleanprelistBusCopy);
					for (int i = 0; i < p1; i++) {
						for (int j = 0; j < p2; j++) {
							for (int k = 0; k < p3; k++) {
								if (booleanprelistMotorcycleCopy[i][j][k][0] != booleanprelistMotorcycle[i][j][k][0]) {
									isSameList = false;
									break;
								}
								if (booleanprelistMotorcycleCopy[i][j][k][1] != booleanprelistMotorcycle[i][j][k][1]) {
									isSameList = false;
									break;
								}
							}
						}
					}
					
					if (otherfield.getText().toString().toLowerCase().equals((fo.getpredamagedItemsOtherField().toLowerCase())))
						isSameText = true;
				} else {
					// isSameList = Arrays.equals(booleanlistBus, booleanlistBusCopy);
					for (int i = 0; i < p1; i++) {
						for (int j = 0; j < p2; j++) {
							for (int k = 0; k < p3; k++) {
								if (booleanlistMotorcycleCopy[i][j][k][0] != booleanlistMotorcycle[i][j][k][0]) {
									isSameList = false;
									break;
								}
								if (booleanlistMotorcycleCopy[i][j][k][1] != booleanlistMotorcycle[i][j][k][1]) {
									isSameList = false;
									break;
								}
							}
						}
					}

					if (otherfield.getText().toString().toLowerCase().equals((fo.getdamagedItemsOtherField().toLowerCase())))
						isSameText = true;
				}

				if (isSameList && isSameText) {
					finish();
					return;
				} else {
					AlertDialog.Builder alertbox = new AlertDialog.Builder(ExpList3_Motorcycle.this);
					alertbox.setTitle(R.string.saform);
					alertbox.setCancelable(false);
					alertbox.setMessage("Do you want to save changes?");
					alertbox.setPositiveButton("Yes",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface arg0, int arg1) {
									okCommon();
									removeDialog(arg1);
									finish();
								}
							});
					alertbox.setNegativeButton("No",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface arg0, int arg1) {
									backCommon();
									removeDialog(arg1);
									finish();
								}
							});
					alertbox.show();
					return;
				}
				
				/*boolean[][][][] a = new boolean[p1][p2][p3][p4];
				if (!fo.getisPreSelected()) {
					// Damaged Items
					fo.setisVehicleShow(true);
					fo.setdamagedItemsOtherField("");
					fo.setpredamagedItemsOtherField("");
					fo.setBooleanlistMotorcycle(a);
					fo.setBooleanprelistMotorcycle(a);
				} else {
					fo.setBooleanprelistMotorcycle(a);
					fo.setpredamagedItemsOtherField(otherfield.getText()
							.toString().trim());
				}*/
			}
			finish();
		} catch (Exception e) {
			LOG.error("multilevel_back_button:10326");
			if (e != null) {
				LOG.error("Message: " + e.getMessage());
				LOG.error("StackTrace: " + e.getStackTrace());
			}
		}
	}
	
	public void on_click_reset_button(View v) {
		try {
			AlertDialog.Builder alertbox = new AlertDialog.Builder(ExpList3_Motorcycle.this);
			alertbox.setTitle(R.string.saform);
			alertbox.setCancelable(false);
			alertbox.setMessage("'Damaged Items' and 'PAD Items' will be reset. Are you sure?");
			alertbox.setPositiveButton("Yes",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface arg0, int arg1) {
							fo.setisVehicleShow(true);
							fo.setdamagedItemsOtherField("");
							fo.setpredamagedItemsOtherField("");
							
							fo.setBooleanlistBus(new boolean[p1][p2][p3][p4]);
							fo.setBooleanlistCar(new boolean[p1][p2][p3][p4]);
							fo.setBooleanlistLorry(new boolean[p1][p2][p3][p4]);
							fo.setBooleanlistVan(new boolean[p1][p2][p3][p4]);
							fo.setBooleanlistThreeWheel(new boolean[p1][p2][p3][p4]);
							fo.setBooleanlistMotorcycle(new boolean[p1][p2][p3][p4]);
							fo.setBooleanlistTractor4WD(new boolean[p1][p2][p3][p4]);
							fo.setBooleanlistHandTractor(new boolean[p1][p2][p3][p4]);
							
							fo.setBooleanprelistBus(new boolean[p1][p2][p3][p4]);
							fo.setBooleanprelistCar(new boolean[p1][p2][p3][p4]);
							fo.setBooleanprelistLorry(new boolean[p1][p2][p3][p4]);
							fo.setBooleanprelistVan(new boolean[p1][p2][p3][p4]);
							fo.setBooleanprelistThreeWheel(new boolean[p1][p2][p3][p4]);
							fo.setBooleanprelistMotorcycle(new boolean[p1][p2][p3][p4]);
							fo.setBooleanprelistTractor4WD(new boolean[p1][p2][p3][p4]);
							fo.setBooleanprelistHandTractor(new boolean[p1][p2][p3][p4]);

							Application.setFormObjectInstance(fo);
							removeDialog(arg1);
							finish();
						}
					});
			alertbox.setNegativeButton("No",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface arg0, int arg1) {
							removeDialog(arg1);
						}
					});
			alertbox.show();
		} catch (Exception e) {
			LOG.error("on_click_reset_button:10772");
			if (e != null) {
				LOG.error("Message: " + e.getMessage());
				LOG.error("StackTrace: " + e.getStackTrace());
			}
		}
	}
	
}